package com.gfxy.modules.common.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;



@Controller("teacherTotalScoreExcelView")
public class TeacherTotalScoreExcelView extends MiniAbstractExcelView {
    public static final String FILE_NAME = "fileName";
    public static final String TITLE = "title";
    public static final String CONTENT = "content";

    public TeacherTotalScoreExcelView() {
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String codedFileName = "临时文件";
        Workbook workbook =  new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();


        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);


        CellStyle cellstyle2 = workbook.createCellStyle();
        cellstyle2.setVerticalAlignment(VerticalAlignment.CENTER);
        cellstyle2.setAlignment(HorizontalAlignment.CENTER);
        cellstyle2.setBorderTop(BorderStyle.THIN);
        cellstyle2.setBorderBottom(BorderStyle.THIN);
        cellstyle2.setBorderLeft(BorderStyle.THIN);
        cellstyle2.setBorderRight(BorderStyle.THIN);


        CellStyle style3 = workbook.createCellStyle();
        style3.setBorderTop(BorderStyle.THIN);
        style3.setBorderBottom(BorderStyle.THIN);
        style3.setBorderLeft(BorderStyle.THIN);
        style3.setBorderRight(BorderStyle.THIN);
        style3.setFillForegroundColor(IndexedColors.CORAL.getIndex());// 设置背景色
        style3.setFillPattern(FillPatternType.SOLID_FOREGROUND);


        String title = (String) model.get(TITLE);
        List<JSONObject> contents = (List<JSONObject>) model.get(CONTENT);

        for(int i=0 ; i<=1; i++){
            Row row = sheet.createRow(i);
            for(int j = 0 ; j <= 13 ;j ++){
                Cell cell = row.createCell(j);
                cell.setCellValue(title);
                cell.setCellStyle(cellstyle2);
            }
        }
        CellRangeAddress region0 = new CellRangeAddress(0, 1, 0, 15);


        //第一行子标题
        this.createSubTitle1(sheet, style);


        this.createSubTitle2(sheet, style);

        CellRangeAddress region1 = new CellRangeAddress(2, 3, 0, 0);
        CellRangeAddress region2 = new CellRangeAddress(2, 3, 1, 1);
        CellRangeAddress region_o2 = new CellRangeAddress(2, 3, 2, 2);
        CellRangeAddress region3 = new CellRangeAddress(2, 3, 3, 3);
        CellRangeAddress region_o3 = new CellRangeAddress(2, 3, 4, 4);
        CellRangeAddress region4 = new CellRangeAddress(2, 2, 5, 6);
        CellRangeAddress region5 = new CellRangeAddress(2, 2, 7, 8);
        CellRangeAddress region6 = new CellRangeAddress(2, 2, 10, 11);
        CellRangeAddress region7 = new CellRangeAddress(2, 2, 12, 13);
        CellRangeAddress region8 = new CellRangeAddress(2, 2, 14, 15);
        sheet.addMergedRegion(region0);
        sheet.addMergedRegion(region1);
        sheet.addMergedRegion(region2);
        sheet.addMergedRegion(region3);
        sheet.addMergedRegion(region_o2);
        sheet.addMergedRegion(region_o3);
        sheet.addMergedRegion(region4);
        sheet.addMergedRegion(region5);
        sheet.addMergedRegion(region6);
        sheet.addMergedRegion(region7);
        sheet.addMergedRegion(region8);

        for(int i = 1; i<=2; i++){
            Row tempRow = sheet.getRow(i);
            for (int j = 0; j < tempRow.getLastCellNum(); j++) {

                Cell cell = tempRow.getCell(j);
                if(cell!=null){
                    cell.setCellStyle(cellstyle2);
                }

            }
        }




        int rowIndex = 4;


        for(int i=0; i< contents.size();i++){

            boolean result = false;
            JSONObject content = contents.get(i);

            Row contentRow = sheet.createRow(rowIndex);


            Cell numCell = contentRow.createCell(0);
            numCell.setCellValue(i+1);
            numCell.setCellStyle(style);

            String teacherNum = content.getString("username");

            Cell teacherNumCell = contentRow.createCell(1);
            teacherNumCell.setCellValue(teacherNum);
            teacherNumCell.setCellStyle(style);


            String username = content.getString("username_dictText");

            Cell userCell = contentRow.createCell(2);
            userCell.setCellValue(username);
            userCell.setCellStyle(style);

            String depName = content.getString("depId_dictText");

            Cell depNameCell = contentRow.createCell(3);
            depNameCell.setCellValue(depName);
            depNameCell.setCellStyle(style);


            String titleRank = content.getString("titleRank_dictText");
            Cell titleRankCell = contentRow.createCell(4);
            titleRankCell.setCellValue(titleRank);
            titleRankCell.setCellStyle(style);

            double classPoint = content.getDouble("classPoint");
            Cell cell = contentRow.createCell(5);
            cell.setCellValue(classPoint);
            cell.setCellStyle(style);

            double classScore = content.getDouble("classScore");
            cell = contentRow.createCell(6);
            cell.setCellValue(classScore);
            cell.setCellStyle(style);

            boolean temp = classPoint > classScore;
            result = result || temp;
            if(temp){
                cell.setCellStyle(style3);
            }


            double buildingPoint = content.getDouble("buildingPoint");
            cell = contentRow.createCell(7);
            cell.setCellValue(buildingPoint);
            cell.setCellStyle(style);

            double buildingScore = content.getDouble("buildingScore");
            cell = contentRow.createCell(8);
            cell.setCellValue(buildingScore);
            cell.setCellStyle(style);


            if(buildingPoint > buildingScore){
                cell.setCellStyle(style3);
            }

            double honourScore = content.getDouble("honourScore");
            cell = contentRow.createCell(9);
            cell.setCellValue(honourScore);
            cell.setCellStyle(style);

            double researchPoint = content.getDouble("researchPoint");
            cell = contentRow.createCell(10);
            cell.setCellValue(researchPoint);
            cell.setCellStyle(style);

            double researchScore = content.getDouble("researchScore");
            cell = contentRow.createCell(11);
            cell.setCellValue(researchScore);
            cell.setCellStyle(style);


            if(researchPoint > researchScore){
                cell.setCellStyle(style3);
            }

            double educationPoint = content.getDouble("educationPoint");
            cell = contentRow.createCell(12);
            cell.setCellValue(educationPoint);
            cell.setCellStyle(style);

            double educationScore = content.getDouble("educationScore");
            cell = contentRow.createCell(13);
            cell.setCellValue(educationScore);
            cell.setCellStyle(style);

            temp = educationPoint > educationScore;
            result = result || temp;
            if(temp){
                cell.setCellStyle(style3);
            }

            double totalPoint = content.getDouble("totalPoint");
            cell = contentRow.createCell(14);
            cell.setCellValue(totalPoint);
            cell.setCellStyle(style);

            double totalScore = content.getDouble("totalScore");
            cell = contentRow.createCell(15);
            cell.setCellValue(totalScore);
            cell.setCellStyle(style);



            if((totalPoint > totalScore) || result){
                cell.setCellStyle(style3);
            }

            rowIndex = rowIndex+1;


        }






        if (model.containsKey(FILE_NAME)) {
            codedFileName = (String)model.get(FILE_NAME);
        }

        codedFileName = codedFileName + ".xls";

        if (this.isIE(request)) {
            codedFileName = URLEncoder.encode(codedFileName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
        }


        response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

    private void createSubTitle1(Sheet sheet, CellStyle style) {



        Row rowSubTitle1 = sheet.createRow(2);

        Cell numCell = rowSubTitle1.createCell(0);
        numCell.setCellValue("序号");
        numCell.setCellStyle(style);

        Cell teacherNumCell = rowSubTitle1.createCell(1);
        teacherNumCell.setCellValue("教工编号");
        teacherNumCell.setCellStyle(style);

        Cell teacherNameCell = rowSubTitle1.createCell(2);
        teacherNameCell.setCellValue("教师姓名");
        teacherNameCell.setCellStyle(style);

        Cell depName = rowSubTitle1.createCell(3);
        depName.setCellValue("部门");
        depName.setCellStyle(style);

        Cell titleRankCell = rowSubTitle1.createCell(4);
        titleRankCell.setCellValue("职称");
        titleRankCell.setCellStyle(style);

        Cell classTimeCell = rowSubTitle1.createCell(5);
        classTimeCell.setCellValue("教学数量");
        classTimeCell.setCellStyle(style);

        Cell tempCell = rowSubTitle1.createCell(6);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);

        Cell teachingBuildCell = rowSubTitle1.createCell(7);
        teachingBuildCell.setCellValue("教学建设");
        teachingBuildCell.setCellStyle(style);

        tempCell = rowSubTitle1.createCell(8);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);

        Cell honourCell = rowSubTitle1.createCell(9);
        honourCell.setCellValue("荣誉奖励");
        honourCell.setCellStyle(style);

        Cell researchCell = rowSubTitle1.createCell(10);
        researchCell.setCellValue("科研");
        researchCell.setCellStyle(style);

        tempCell = rowSubTitle1.createCell(11);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);

        Cell educationCell = rowSubTitle1.createCell(12);
        educationCell.setCellValue("育人与服务");
        educationCell.setCellStyle(style);

        tempCell = rowSubTitle1.createCell(13);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);

        Cell totalCell = rowSubTitle1.createCell(14);
        totalCell.setCellValue("总分");
        totalCell.setCellStyle(style);

        tempCell = rowSubTitle1.createCell(15);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
    }

    private void createSubTitle2(Sheet sheet, CellStyle style) {
        Row rowSubTitle2 = sheet.createRow(3);


        Cell tempCell = rowSubTitle2.createCell(0);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(1);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(2);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(3);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);
        tempCell = rowSubTitle2.createCell(4);
        tempCell.setCellValue("");
        tempCell.setCellStyle(style);

        Cell classTimePointCell = rowSubTitle2.createCell(5);
        classTimePointCell.setCellValue("指标");
        classTimePointCell.setCellStyle(style);

        Cell classTimeScoreCell = rowSubTitle2.createCell(6);
        classTimeScoreCell.setCellValue("积分");
        classTimeScoreCell.setCellStyle(style);

        Cell classTimeBuildPointCell = rowSubTitle2.createCell(7);
        classTimeBuildPointCell.setCellValue("指标");
        classTimeBuildPointCell.setCellStyle(style);



        Cell teachingBuildScoreCell = rowSubTitle2.createCell(8);
        teachingBuildScoreCell.setCellValue("积分");
        teachingBuildScoreCell.setCellStyle(style);


        Cell honourScoreCell = rowSubTitle2.createCell(9);
        honourScoreCell.setCellValue("积分");
        honourScoreCell.setCellStyle(style);

        Cell researchPointCell = rowSubTitle2.createCell(10);
        researchPointCell.setCellValue("指标");
        researchPointCell.setCellStyle(style);


        Cell researchScoreCell = rowSubTitle2.createCell(11);
        researchScoreCell.setCellValue("积分");
        researchScoreCell.setCellStyle(style);

        Cell educationPointCell = rowSubTitle2.createCell(12);
        educationPointCell.setCellValue("指标");
        educationPointCell.setCellStyle(style);

        Cell educationScoreCell = rowSubTitle2.createCell(13);
        educationScoreCell.setCellValue("积分");
        educationScoreCell.setCellStyle(style);

        Cell totalPointCell = rowSubTitle2.createCell(14);
        totalPointCell.setCellValue("指标");
        totalPointCell.setCellStyle(style);

        Cell totalScoreCell = rowSubTitle2.createCell(15);
        totalScoreCell.setCellValue("积分");
        totalScoreCell.setCellStyle(style);
    }
}
